<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="订单创建日期" v-bind="validateInfos.orderCreateTime">
		        <a-date-picker placeholder="请选择订单创建日期" v-model:value="formData.orderCreateTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="下单时间" v-bind="validateInfos.applyTime">
            <a-input v-model:value="formData.applyTime" placeholder="请输入下单时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="支付时间" v-bind="validateInfos.payTime">
		        <a-date-picker placeholder="请选择支付时间" v-model:value="formData.payTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款人名称" v-bind="validateInfos.payAccountName">
            <a-input v-model:value="formData.payAccountName" placeholder="请输入付款人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="被保人" v-bind="validateInfos.insuredName">
            <a-input v-model:value="formData.insuredName" placeholder="请输入被保人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="出保时间" v-bind="validateInfos.insureActiveDate">
		        <a-date-picker placeholder="请选择出保时间" v-model:value="formData.insureActiveDate" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="订单号" v-bind="validateInfos.orderNo">
            <a-input v-model:value="formData.orderNo" placeholder="请输入订单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保单号" v-bind="validateInfos.policyNo">
            <a-input v-model:value="formData.policyNo" placeholder="请输入保单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="支付状态" v-bind="validateInfos.payState">
            <a-input v-model:value="formData.payState" placeholder="请输入支付状态" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险状态" v-bind="validateInfos.policyState">
            <j-dict-select-tag v-model:value="formData.policyState" dictCode="DZBDZT" placeholder="请选择保险状态"   :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="订单来源" v-bind="validateInfos.sourceType">
            <j-dict-select-tag v-model:value="formData.sourceType" dictCode="PPYWLX" placeholder="请输入订单来源"  :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保费" v-bind="validateInfos.cost">
	          <a-input-number v-model:value="formData.cost" placeholder="请输入保费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="平台" v-bind="validateInfos.platformCode">
            <j-dict-select-tag v-model:value="formData.platformCode" dictCode="JYPTBM" placeholder="请输入平台"  :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险公司" v-bind="validateInfos.insuranceCompany">
            <j-dict-select-tag v-model:value="formData.insuranceCompany" dictCode="BXGS" placeholder="请选择保险公司"   :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="渠道商" v-bind="validateInfos.channelProvider">
            <a-input v-model:value="formData.channelProvider" placeholder="请输入渠道商" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="费率" v-bind="validateInfos.rate">
	          <a-input-number v-model:value="formData.rate" placeholder="请输入费率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="地区" v-bind="validateInfos.area">
            <a-input v-model:value="formData.area" placeholder="请输入地区" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保证金" v-bind="validateInfos.margin">
	          <a-input-number v-model:value="formData.margin" placeholder="请输入保证金" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务人员名称" v-bind="validateInfos.salesmanName">
            <a-input v-model:value="formData.salesmanName" placeholder="请输入业务人员名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="渠道名称" v-bind="validateInfos.channelName">
            <a-input v-model:value="formData.channelName" placeholder="请输入渠道名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="不含税保费" v-bind="validateInfos.exTaxPremium">
	          <a-input-number v-model:value="formData.exTaxPremium" placeholder="请输入不含税保费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险公司经纪费比例" v-bind="validateInfos.insuranceFeeRate">
	          <a-input-number v-model:value="formData.insuranceFeeRate" placeholder="请输入保险公司经纪费比例" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险公司经纪费" v-bind="validateInfos.insuranceFee">
	          <a-input-number v-model:value="formData.insuranceFee" placeholder="请输入保险公司经纪费比例" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="渠道经纪费比例" v-bind="validateInfos.channelFeeRate">
	          <a-input-number v-model:value="formData.channelFeeRate" placeholder="请输入渠道经纪费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="渠道经纪费比例" v-bind="validateInfos.channelFee">
	          <a-input-number v-model:value="formData.channelFee" placeholder="请输入渠道经纪费比例" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务员经纪费比例" v-bind="validateInfos.salesmanFeeRate">
	          <a-input-number v-model:value="formData.salesmanFeeRate" placeholder="请输入业务员经纪费比例" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务员经纪费" v-bind="validateInfos.salesmanFee">
	          <a-input-number v-model:value="formData.salesmanFee" placeholder="请输入业务员经纪费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="系统服务商经纪费" v-bind="validateInfos.systemFee">
	          <a-input-number v-model:value="formData.systemFee" placeholder="请输入系统服务商经纪费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保人名称" v-bind="validateInfos.policyHolderName">
            <a-input v-model:value="formData.policyHolderName" placeholder="请输入投保人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目名称" v-bind="validateInfos.projectName">
            <a-input v-model:value="formData.projectName" placeholder="请输入项目名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="系统服务商纪费比例" v-bind="validateInfos.systemRate">
	          <a-input-number v-model:value="formData.systemRate" placeholder="请输入系统服务商纪费比例" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { Form } from 'ant-design-vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    orderCreateTime: '',   
    applyTime: '',   
    payTime: '',   
    payAccountName: '',   
    insuredName: '',   
    insureActiveDate: '',   
    orderNo: '',   
    policyNo: '',   
    payState: '',   
    policyState: '',   
    sourceType: '',   
    cost: undefined,
    platformCode: '',   
    insuranceCompany: '',   
    channelProvider: '',   
    rate: undefined,
    area: '',   
    margin: undefined,
    channelSettlementId: '',   
    salesmanSettlementId: '',   
    insuranceSettlementId: '',   
    salesmanId: '',   
    salesmanName: '',   
    channelName: '',   
    exTaxPremium: undefined,
    insuranceFeeRate: undefined,
    insuranceFee: undefined,
    channelFeeRate: undefined,
    channelFee: undefined,
    salesmanFeeRate: undefined,
    salesmanFee: undefined,
    systemFee: undefined,
    policyHolderName: '',   
    projectName: '',   
    systemRate: undefined,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 10 }, style: { width: '20px' } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 14 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    orderCreateTime: [{ required: true, message: '请输入订单创建日期!'},],
    insuredName: [{ required: true, message: '请输入被保人!'},],
    payState: [{ required: true, message: '请输入支付状态!'},],
    policyState: [{ required: true, message: '请输入保险状态!'},],
    sourceType: [{ required: true, message: '请输入订单来源!'},],
    cost: [{ required: true, message: '请输入保费!'},],
    platformCode: [{ required: true, message: '请输入平台!'},],
    insuranceCompany: [{ required: true, message: '请输入保险公司!'},],
    channelProvider: [{ required: true, message: '请输入渠道商!'},],
    channelSettlementId: [{ required: true, message: '请输入渠道结算订单id!'},],
    salesmanSettlementId: [{ required: true, message: '请输入业务结算汇总订单id!'},],
    insuranceSettlementId: [{ required: true, message: '请输入保险公司结算汇总订单id!'},],
    policyHolderName: [{ required: true, message: '请输入投保人名称!'},],
    projectName: [{ required: true, message: '请输入项目名称!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
